package com.zhu.library_management_system.controller;

import com.zhu.library_management_system.pojo.Book;
import com.zhu.library_management_system.pojo.BookBorrowing;
import com.zhu.library_management_system.responseMessage.ResponseMessage;
import com.zhu.library_management_system.service.BookService;
import com.zhu.library_management_system.service.UserService;
import com.zhu.library_management_system.util.RoleVerify;
import com.zhu.library_management_system.util.VerifyLoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@RestController
@RequestMapping("/user/book")
@Api(tags = "书籍管理接口")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    @GetMapping("/findAllBooks")
    @ApiOperation("获取所有书籍信息")
    public ResponseMessage<List<Book>> findAllBooks(){
        Iterable<Book> book = bookService.findAllBooks();
        List<Book> bookList = StreamSupport.stream(book.spliterator(), false)
                .collect(Collectors.toList());
        if (bookList.size() == 0){
            return ResponseMessage.success(200, "当前还没有书籍", null);
        }
        return ResponseMessage.success(bookList);
    }

    @GetMapping("/findBookById/{bookId}")
    @ApiOperation("根据书籍ID获取书籍信息")
    public ResponseMessage<Book> findBookById(@PathVariable Integer bookId){
        try {
            Book book = bookService.findBookById(bookId);
            return ResponseMessage.success(book);
        }catch (RuntimeException e){
            return ResponseMessage.success(404, e.getMessage(), null);
        }
    }

    @GetMapping("/findBookByTitle/{title}")
    @ApiOperation("根据书籍名获取书籍信息")
    public ResponseMessage<Book> findBookByTitle(@PathVariable String title){
        if (bookService.findBookByTitle(title) == null){
            return ResponseMessage.success(404, "书籍不存在", null);
        }
        return ResponseMessage.success(bookService.findBookByTitle(title));
    }

    @PostMapping("/addBook")
    @ApiOperation("添加书籍信息")
    public ResponseMessage<Book> addBook(@RequestHeader("Authorization") String token, @RequestBody Book book){
        Integer loginId = VerifyLoginUtil.verifyLogin(token);
        if (loginId == null || VerifyLoginUtil.isTokenValid(token)){
            return ResponseMessage.success(401, "Token无效或已过期", null);
        }

        if (RoleVerify.verifyRole(userService.getRole(loginId).getRole()).equals(null) || !RoleVerify.verifyRole(userService.getRole(loginId).getRole()).equals(0)){
            return ResponseMessage.success(403, "抱歉，你无权进行此操作！", null);
        }

        bookService.addBook(book);
        return ResponseMessage.success(null);
    }

    @PutMapping("/updateBook/{id}")
    @ApiOperation("更新书籍信息")
    public ResponseMessage<Book> updateBook(@RequestHeader("Authorization") String token, @PathVariable Integer id, @RequestBody Book book){
        Integer loginId = VerifyLoginUtil.verifyLogin(token);
        if (loginId == null || VerifyLoginUtil.isTokenValid(token)){
            return ResponseMessage.success(401, "Token无效或已过期", null);
        }

        if (RoleVerify.verifyRole(userService.getRole(loginId).getRole()).equals(null) || !RoleVerify.verifyRole(userService.getRole(loginId).getRole()).equals(0)){
            return ResponseMessage.success(403, "抱歉，你无权进行此操作！", null);
        }

        book.setId(id);
        bookService.updateBook(book);
        return ResponseMessage.success(null);
    }

    @DeleteMapping("/deleteBook/{id}")
    @ApiOperation("删除书籍信息")
    public ResponseMessage<Book> deleteBook(@RequestHeader("Authorization") String token, @PathVariable Integer id){
        Integer loginId = VerifyLoginUtil.verifyLogin(token);
        if (loginId == null || VerifyLoginUtil.isTokenValid(token)){
            return ResponseMessage.success(401, "Token无效或已过期", null);
        }

        if (RoleVerify.verifyRole(userService.getRole(loginId).getRole()).equals(null) || !RoleVerify.verifyRole(userService.getRole(loginId).getRole()).equals(0)){
            return ResponseMessage.success(403, "抱歉，你无权进行此操作！", null);
        }

        bookService.deleteBook(id);
        return ResponseMessage.success(null);
    }
}
